Skip to content

Refactor image embedding logic - #406

Merged
martinRenou merged 1 commit into
matplotlib:masterfrom
martinRenou:image_embedding_refactor
Dec 15, 2021
Merged

martinRenou merged 1 commit into
matplotlib:masterfrom
martinRenou:image_embedding_refactor

Conversation

@martinRenou

@martinRenou martinRenou commented Dec 14, 2021 •

Copy link
Copy Markdown
Member

Refactor the image embedding logic.

This fixes an issue in the case of nbconvert --to html that was preventing the plot image to render.

@github-actions

Copy link
Copy Markdown
Contributor

Binder 👈 Launch a binder notebook on branch martinRenou/ipympl/image_embedding_refactor

@martinRenou
martinRenou force-pushed the image_embedding_refactor branch 5 times, most recently from d149e3e to 7c997b4 Compare December 14, 2021 12:11
@martinRenou
martinRenou force-pushed the image_embedding_refactor branch from 7c997b4 to 9aad6fc Compare December 14, 2021 12:27
@martinRenou
martinRenou marked this pull request as ready for review December 14, 2021 12:29
Comment thread ipympl/backend_nbagg.py
)

# Update the widget model properly for HTML embedding
self._size = (width, height)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one important change here.

We need to make the plot size part of the widget model in the case of nbconvert --to html otherwise there is no registered size and the canvas would be 0 sized.

Comment thread src/mpl_widget.ts
this.image.onload = () => {
// In case of an embedded widget, the initial size is not correct
// and we are not receiving any resize event from the server
if (this.disabled) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the second important change in this PR.

Here, we need a special case for when the widget is disabled (no counterpart in the kernel, or no kernel at all), where we display the image that is part of the model.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right that this will come up in many situations, not just nbconvert, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. You can trigger this situation by saving the widget model in the page, shutdown the kernel, then refresh the page.

Comment thread ipympl/backend_nbagg.py

_width = CInt().tag(sync=True)
_height = CInt().tag(sync=True)
_size = Tuple([0, 0]).tag(sync=True)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing the two private traits _width and _height into _size so that the resize happens in one comm message

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

@ianhi

ianhi commented Dec 14, 2021

Copy link
Copy Markdown
Collaborator

Haven't tried it out, but this all looks pretty sensible to me.

@martinRenou

Copy link
Copy Markdown
Member Author

Thanks for looking into it!

I will merge and make a patch release with this soonish.

@martinRenou
martinRenou merged commit 480a236 into matplotlib:master Dec 15, 2021
@martinRenou
martinRenou deleted the image_embedding_refactor branch December 15, 2021 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants